package com.zbkj.crmeb.store.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zbkj.crmeb.store.dao.ProductInformationDao;
import com.zbkj.crmeb.store.model.ProductInformation;
import com.zbkj.crmeb.store.request.ProductInformationRequest;
import com.zbkj.crmeb.store.service.IProductInformationService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 商品信息表 服务实现类
 * </p>
 *
 * @author xx
 * @since 2022-05-12
 */
@Service
public class ProductInformationServiceImpl extends ServiceImpl<ProductInformationDao, ProductInformation> implements IProductInformationService {

    @Override
    public Boolean saveOrUpdInformation(ProductInformation productInformation) {
        if (Objects.isNull(productInformation) || StringUtils.isBlank(productInformation.getProductInformation())){
            throw new CrmebException("商品信息不能为空");
        }
        if (Objects.isNull(productInformation.getId())){
            productInformation.setCreateTime(LocalDateTime.now());
        }
        return this.saveOrUpdate(productInformation);
    }

    @Override
    public CommonPage<ProductInformation> getInformationPage(ProductInformationRequest request, PageParamRequest pageParamRequest) {
        Page<ProductInformation> page = PageHelper.startPage(pageParamRequest.getPage(),pageParamRequest.getLimit());
        List<ProductInformation> productInformationList = this.list(Wrappers.lambdaQuery(ProductInformation.class)
                .like(StringUtils.isNotBlank(request.getProductInformation()),ProductInformation::getProductInformation,request.getProductInformation())
                .orderByDesc(ProductInformation::getId));
        return CommonPage.restPage(CommonPage.copyPageInfo(page, productInformationList));
    }

    @Override
    public List<ProductInformation> getInformationList() {
        return this.list(Wrappers.lambdaQuery(ProductInformation.class)
                .orderByDesc(ProductInformation::getId));
    }

    @Override
    public Boolean delInformation(Integer id) {
        return this.removeById(id);
    }
}
